/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.custom.hvac;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.hvac.FanSignalController;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class AutoFanControlView extends LinearLayout implements RadioGroup.OnCheckedChangeListener {

    private static final String TAG = "AutoFanControlView";

    private LinearLayout rootView;
    private RadioGroup rgAutoFan;

    private FanSignalController mFanSignalController;

    public AutoFanControlView(Context context) {
        this(context, null);
    }

    public AutoFanControlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoFanControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        mFanSignalController = FanSignalController.getInstance();

        LayoutInflater.from(getContext()).inflate(R.layout.dialog_auto_fan_speed, this, true);

    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if(visibility == View.VISIBLE){
            boolean isSupportAutoFan = mFanSignalController.isSupportAutoFan();
            LogUtils.d(TAG, "isSupportAutoFan:" + isSupportAutoFan);
            if (isSupportAutoFan) {
                Integer autoFanValueLevel = mFanSignalController.getAutoFanValueLevel();
                updateProgress(autoFanValueLevel);
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        rootView = findViewById(R.id.root_view);
        rgAutoFan = findViewById(R.id.rg_auto_fan);

        rgAutoFan.setOnCheckedChangeListener(this);
    }

    public void startTimingClose() {

    }

    public void updateProgress(Integer value) {
        LogUtils.d(TAG, "updateProgress value:" + value);
        if (rgAutoFan != null) {
            int checkId = getRgCheckedIdByAutoValue(value);
            rgAutoFan.setOnCheckedChangeListener(null);
            rgAutoFan.check(checkId);
            rgAutoFan.setOnCheckedChangeListener(this);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        startTimingClose();

        int value = getAutoValueByChecked(checkedId);
        LogUtils.d(TAG, "Set Auto Value level :" + value);
        if (mFanSignalController == null) {
            return;
        }
        mFanSignalController.setAutoFanValue(value);
    }

    private int getAutoValueByChecked(@IdRes int checkedId) {
        if (R.id.rb_auto_fan2 == checkedId) {
            return AppConstants.PRO_AUTO_FAN_SPEED_10;
        } else if (R.id.rb_auto_fan3 == checkedId) {
            return AppConstants.PRO_AUTO_FAN_SPEED_11;
        } else if (R.id.rb_auto_fan4 == checkedId) {
            return AppConstants.PRO_AUTO_FAN_SPEED_12;
        } else if (R.id.rb_auto_fan1 == checkedId) {
            return AppConstants.PRO_AUTO_FAN_SPEED_13;
        } else {
            return AppConstants.PRO_AUTO_FAN_SPEED_14;
        }
    }

    private @IdRes
    int getRgCheckedIdByAutoValue(Integer value) {
        switch (value) {
            case AppConstants.HVAC_AUTO_FAN_SPEED_10:
                return R.id.rb_auto_fan2;
            case AppConstants.HVAC_AUTO_FAN_SPEED_11:
                return R.id.rb_auto_fan3;
            case AppConstants.HVAC_AUTO_FAN_SPEED_12:
                return R.id.rb_auto_fan4;
            case AppConstants.HVAC_AUTO_FAN_SPEED_13:
                return R.id.rb_auto_fan1;
            case AppConstants.HVAC_AUTO_FAN_SPEED_14:
                return R.id.rb_auto_fan5;
        }
        LogUtils.d(TAG, "getRgCheckedIdByAutoValue NO NO AUTO VALUE");
        return R.id.rb_auto_fan1;
    }
}
